"use strict";

/*************************************************************
KODAX-WIDGETS-PLUGIN SCRIPTS

TWITTER FEED WITH THEME DESIGN
*************************************************************/


/*************************************************************
TWITTER FEED WITH THEME DESIGN
*************************************************************/

	jQuery(document).ready(function($) {
		if ($('.twitter_theme_design').size() > 0) {

			var $twitterThemeDesignContainer = $('.twitter_theme_design');

			$twitterThemeDesignContainer.each(function(index) {

				var $this = $(this);

				var useThemeDesign = $this.attr('data-theme_design');
				if (useThemeDesign == "false") {
					$this.hide();
				} else {
					var $associatedTwitterWidget = $this.prev('.twitter_widget');
					$associatedTwitterWidget.hide();

					$(window).load(function() {
						//set vars
						var success = false;
						var delay = 100;
						var attempts = 10;

						for (var $i = 1; $i < attempts+1; $i++) {  
									
								setTimeout(function() {
									if (success === false) {
										var $twitterIframe = $this.prev('.twitter_widget').find('iframe');
									    if ($twitterIframe.contents().find('.tweet').size() > 0) {
									    	success = true;	

											//get and post tweets
											var numTweets = $this.attr('data-num_tweets');
											var tweetCount = 0;
											$twitterIframe.contents().find('.tweet').each(function(index, e){
												if (tweetCount == numTweets) return;
												var $this = $(this);
												var published = $this.find('time').text();
												var tweet = $this.find('.e-entry-title').html();
												var altTweet = "<li class='tweet'><p>" + tweet + "</p><h6 class='meta'>" + published + "</span></li>";
												var $associatedTwitterThemeDesignContainer = $twitterIframe.closest('.twitter_widget').next('.twitter_theme_design');
												$associatedTwitterThemeDesignContainer.find('ul').append(altTweet);
												tweetCount++;
											});
									    }
									}

								}, delay*$i);

						} // end fori

					}); // end on window load

				} // end if else

			}); // end each instance

		}

	});

